package com.example.testapplication;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
 *  慕课自定义组件
 *  1.创建组件
 *  2.在res/values/attrs.xml
 *
 * */
import androidx.annotation.Nullable;

public class TestView extends View {
    private String mText = "Imooc";
    private Paint mPaint;
    public TestView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        // 获取 xml声明的属性值
        // 这种会丢失默认值
        // 自定义属性的声明与获取
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.TestView);
        boolean booleanTest = ta.getBoolean(R.styleable.TestView_test_boolean,false);
        int integerTest = ta.getInteger(R.styleable.TestView_test_integer,1);
        float dimensionTest = ta.getDimension(R.styleable.TestView_test_dimension,0);
        int enumTest = ta.getInt(R.styleable.TestView_test_enum,1);
       // mText = ta.getString(R.styleable.TestView_test_string); // null

        // 这种不会影响村存量变量
        int count = ta.getIndexCount();
        for(int i =0;i<count;i++){
            int index = ta.getIndex(i);
            switch (index){
                case R.styleable.TestView_test_string:
                    mText = ta.getString(R.styleable.TestView_test_string);
                    break;
            }
        }

        Log.e("Tag",booleanTest+
                ","+integerTest+","
                +dimensionTest+","+enumTest+","+mText);
        ta.recycle();

    }

    //测量onMeasure

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int width  = 0;
        if(widthMode == MeasureSpec.EXACTLY){
            width = widthSize;
        }else{
            int needWidth = measureWidth() + getPaddingLeft() + getPaddingRight();
            if(widthMode == MeasureSpec.AT_MOST){
                width = Math.min(needWidth,widthSize);

            }else{
                width = needWidth;
            }
        }
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int height = 0;
        if(heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }else{
            int needHeight = measureHeight() + getPaddingTop() + getPaddingBottom();
            if(heightMode == MeasureSpec.AT_MOST){
                height = Math.min(needHeight,heightSize);
            }else{
                height = needHeight;
            }
        }

        setMeasuredDimension(width,height);
    }

    private int measureWidth(){
        return 0;
    }
    private int measureHeight(){
        return 0;
    }

    private void initPaint(){
        mPaint = new Paint();  // 画饼
        mPaint.setStyle(Paint.Style.STROKE); // 填充
        mPaint.setStrokeWidth(6); // 宽度
        mPaint.setColor(0xFFFF0000); // 颜色
        mPaint.setAntiAlias(true); //
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 画圆
        canvas.drawCircle(getWidth()/2 ,getHeight()/2,getWidth()/2 - mPaint.getStrokeWidth()/2,mPaint);
        mPaint.setStrokeWidth(1);
        canvas.drawLine(0,getHeight()/2,getWidth(),getHeight()/2,mPaint);
        canvas.drawLine(getWidth()/2,0,getWidth()/2,getHeight(),mPaint);
        mPaint.setTextSize(100); // 设置大小
       // mPaint.setStyle(Paint.Style.FILL);// 填充
        mPaint.setStrokeWidth(0);
        canvas.drawText(mText,0,mText.length(),0,getHeight(),mPaint); //  添加文本
       // super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mText = "88888";
        invalidate();
        return super.onTouchEvent(event);
    }

    private static final String INSTANCE = "instance";
    private static final String KEY_TEXT = "key_text";

    // 状态的存储与恢复


    @Nullable
    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TEXT,mText);
        bundle.putParcelable(INSTANCE,super.onSaveInstanceState());
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if(state instanceof Bundle){
            Bundle bundle = (Bundle) state;
            Parcelable parcelable = bundle.getParcelable(INSTANCE);
            super.onRestoreInstanceState(parcelable);
            return;
        }
        super.onRestoreInstanceState(state);
    }
}
